import api from '../../../api/index'
import uploadOss from '../../../utils/uploadFileOss/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    contact: '',
    fileList: []
  },
  //文件选择
  afterRead(event) {
    const {file} = event.detail;
    console.log(file);
    var num = []
    num.push(file.url)
    console.log(num);
    console.log(num);
    this.uploadDIY(
        num,
        0,
        0,
        0,
        num.length,
        num
    ) 
},
/* 删除图片*/
deleteImg(e) {
    let fileList = this.data.fileList;
    fileList.splice(e.detail.index, 1);
    this.setData({fileList: fileList});
},
uploadDIY(filePaths, successUp, failUp, i, length, param) {
  // 正在上传
  // debugger
  param.doing = true // this.data.prove.doing = true;
  param.imagesUrl = []
  let that = this
  // wx.showLoading({
  //     title: `上传${i}/${length}张图片中`,
  //     duration: 1000
  // })

  uploadOss(filePaths[i])
      .then((res) => {
          successUp++
          i++
          param.imagesUrl.push(res.url)
          if (i === length) {
              // 全部上传成功
              console.log('图片上传成功后的url', param.imagesUrl, length)
              if (successUp === length) {
                  param.uploadSuccess = true
                  console.log('图片上传成功进入回调')
                  let img = {
                    url: param.imagesUrl,
                    name: '图片1',
                }
                let fileList = this.data.fileList;
                  fileList.push(img);
                  this.setData({fileList: fileList});
                  // wx.hideLoading()
                  // that.savetmp(param.imagesUrl[0])
              } else {
                  // 图片上传部分失败
                  param.doing = false
                  wx.hideLoading()
                  wx.showToast({
                      title: '上传图片失败,请重新上传！',
                      icon: 'none',
                      duration: 2000
                  })
              }
          } else {
              // 递归调用uploadDIY函数
              that.uploadDIY(
                  filePaths,
                  successUp,
                  failUp,
                  i,
                  length,
                  param
              )
          }
      })
      .catch((e) => {
          console.log('---图片上传失败 catch--- e', e)
          param.doing = false
          failUp++
          wx.hideLoading()
          wx.showToast({
              title: '图片上传失败，请重新上传！',
              icon: 'none',
              duration: 2000
          })
      })
},
 
  formSubmit: function (e) {
    let _that = this;
    let content = e.detail.value.opinion;
    let contact = e.detail.value.contact;
    let regPhone = /^1[3578]\d{9}$/;
    let regEmail = /^[a-z\d_\-\.]+@[a-z\d_\-]+\.[a-z\d_\-]+$/i;
    if (content == "") {
      wx.showModal({
        title: '提示',
        content: '反馈内容不能为空!',
      })
      return false
    }
    // if (contact == "") {
    //   wx.showModal({
    //     title: '提示',
    //     content: '手机号或者邮箱不能为空!',
    //   })
    //   return false
    // }
    // if(contact == "" && content == "") {
    //   wx.showModal({
    //     title: '提示',
    //     content: '反馈内容,手机号或者邮箱不能为空!',
    //   })
    //   return false
    // }
    // if ((!regPhone.test(contact) && !regEmail.test(contact)) || (regPhone.test(contact) && regEmail.test(contact))) { //验证手机号或者邮箱的其中一个对
    //   wx.showModal({
    //     title: '提示',
    //     content: '您输入的手机号或者邮箱有误!',
    //   })
    //   return false
    // } 
    else {
      this.setData({
        loading: true
      })

      var n = wx.getStorageSync("userinfo");

      let nickname;
    
      //当本地缓存的用户名称不为""或者null时，设置userinfo信息
      if(n.nickName != '' && n.nickName != null){
          nickname = n.nickName;
      }
      console.log('====================================');
    console.log(this.data.fileList);
    console.log('====================================');
      
    let status = false;
      api.post('/api/hxhealth/feedback/save', {
        data: {
          content,
          images: JSON.stringify(this.data.fileList)
        }
    }).then(res => {
        wx.showToast({
            title: '反馈成功',
        })
        //提示框消失后返回上一级页面
        setTimeout(() => {
          wx.navigateBack({
            change: true,
          })
      }, 1200)
    }).catch(e => {
        console.log(e)
        wx.showToast({
          icon: 'error',
          duration: 1200,
          title: '反馈失败：' + e.faultstring,
        })
    })
      return status;
    }
  },
})